package com.tianyi.sfzy1.controller;

import com.tianyi.sfzy1.domain.Announcement;
import com.tianyi.sfzy1.service.AnnouncementService;
import com.tianyi.sfzy1.util.R;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;
    //删除公告
    @DeleteMapping
    public R deleteAnnouncement(Long id) {
        int i = announcementService.deleteAnnouncement(id);
        if (i == 1) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    //批量删除
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        System.out.println(ids);
        int i = announcementService.deleteBatch(ids);
        if (i > 0) {
            return R.ok("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
    //插入公告
    @PostMapping
    public R insertAnnouncement( @RequestBody Announcement announcement) {
        System.out.println(announcement);
        int i = announcementService.insertAnnouncement(announcement);
        if (i > 0) {
            return R.ok("插入成功");
        } else {
            return R.error("插入失败");
        }
    }

    //修改公告
    @PutMapping
    public R updateAnnouncement(@RequestBody Announcement announcement) {
        int i = announcementService.updateAnnouncement(announcement);
        System.out.println("这是修改的信息");
        System.out.println(announcement);
        if (i == 1) {
            return R.ok("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
    //    咨询师跟公告表联查
    @GetMapping("/list")
    public List<Announcement> searchAnnouncements(@RequestParam(required = false) String title, @RequestParam(required = false) String consultantRealname) {
        System.out.println(title);
        System.out.println(consultantRealname);
        return announcementService.findAnnouncementsByTitleOrConsultant(title, consultantRealname);
    }
}
